package com.bls.cate.ui.activity.login;

import android.content.Intent;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.EmptyUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bls.blslib.base.BaseActivity;
import com.bls.blslib.eventbus.Event;
import com.bls.blslib.eventbus.EventBusUtils;
import com.bls.cate.R;
import com.bls.cate.constant.EventBusCode;
import com.bls.cate.constant.Interface;
import com.bls.cate.listener.EditTextChangedListener;
import com.bls.cate.model.LoginRes;
import com.bls.cate.ui.activity.register.RegisterActivity;
import com.bls.cate.utils.AccountUtil;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.xiaomi.mipush.sdk.MiPushClient;

import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/6.
 */

public class LoginActivity extends BaseActivity {
    public static final int GET_RESULT_CODE = 100;
    private EditText etPhone, etPwd;
    private ImageView btnTopTitleLeft2, btnCancel;
    private TextView btnPwd, btnLogin, btnRegister;
    private LoginActivity mActivity;

    @Override
    protected void initParams() {
        super.initParams();
        mActivity = (LoginActivity) getActivity();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        TextView tvTitle = findViewById(R.id.title_middle_text);
        tvTitle.setText("登陆");
        btnTopTitleLeft2 = findViewById(R.id.title_left_iv_2);
        btnTopTitleLeft2.setVisibility(View.VISIBLE);

        etPhone = findViewById(R.id.et_phone);
        btnCancel = findViewById(R.id.btn_iv_cancel);
        etPwd = findViewById(R.id.et_pwd);
        btnPwd = findViewById(R.id.btn_pwd);
        btnLogin = findViewById(R.id.btn_login);
        btnRegister = findViewById(R.id.btn_register);

        btnLogin.setBackground(getDrawable(R.drawable.bg_btn_login_register_2));
        String strBtnRegister = "<font>还没有账号？ </font><u><font color='#66CCFF'>去注册</font></u>";
        btnRegister.setText(Html.fromHtml(strBtnRegister));
    }

    @Override
    protected void initListener() {
        setOnClickListener(btnCancel, btnPwd, btnLogin, btnRegister, btnTopTitleLeft2);
        EditTextChangedListener editTextChangedListener = new EditTextChangedListener(etPhone, etPwd, btnLogin, getActivity());
        etPhone.addTextChangedListener(editTextChangedListener);
        etPwd.addTextChangedListener(editTextChangedListener);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_iv_2:
                finish();
                break;
            case R.id.btn_iv_cancel:
                etPhone.setText(null);
                break;
            case R.id.btn_pwd:
                ToastUtils.showLong(R.string.to_be_continued);
                break;
            case R.id.btn_login:
                btnEventLogin();
                break;
            case R.id.btn_register:
                Intent intent = new Intent();
                intent.setClass(getActivity(), RegisterActivity.class);
                startActivityForResult(intent, GET_RESULT_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GET_RESULT_CODE && resultCode == RegisterActivity.SET_RESULT_CODE) {
            String phone = data.getStringExtra("phone");
            String password = data.getStringExtra("password");
            etPhone.setText(phone);
            etPwd.setText(password);
        }
    }

    /**
     * 登陆事件
     */
    private void btnEventLogin() {
        String strPhone = etPhone.getText().toString().trim();
        String strPwd = etPwd.getText().toString().trim();
        if (EmptyUtils.isEmpty(strPhone) || EmptyUtils.isEmpty(strPwd)) {
            ToastUtils.showShort("请输入账号密码");
        }
       /* else if (!RegexUtils.isMobileSimple(strPhone)) {
            ToastUtils.showShort("手机号长度");
        } else if (strPwd.length() < 6) {
            ToastUtils.showShort("密码长度至少6位");
        } */
        else {
            presenter.requestStrData(0, Interface.INTERFACE_ACCOUNT, CacheMode.NO_CACHE,
                    new HttpParams("RequestMethod", "Login"),
                    new HttpParams("Phone", strPhone),
                    new HttpParams("Password", strPwd)
            );
        }
    }

    @Override
    public void showOnStart(Request request, int requestId) {
        loadingDogDialog.show();
    }

    @Override
    public void showOnSuccess(int requestId, String responseBody, Response response) {
        super.showOnSuccess(requestId, responseBody, response);
        if (requestId == 0) {
            LoginRes loginRes = gson.fromJson(responseBody, LoginRes.class);
            ToastUtils.showShort(loginRes.getResultDes());
            if (loginRes.isResultStatus()) {
                LoginRes.ResultDataBean resultDataBean = loginRes.getResultData();
                MiPushClient.setUserAccount(getApplicationContext(), resultDataBean.getUniqueId(), null);
                AccountUtil.writeLoginSp(resultDataBean);
                EventBusUtils.sendStickyEvent(new Event(EventBusCode.LOGIN_SUCCESS_REFRESH_DATE));

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mActivity.finish();
                    }
                }, 1500);
            }
        }
    }

    @Override
    public void showOnFinish(int requestId) {
        loadingDogDialog.dismiss();
    }


}
